package com.zr.inquiry.service.impl;

import com.zr.inquiry.mapper.InquiryMapper;
import com.zr.inquiry.meiju.CurrencyCodeEnum;
import com.zr.inquiry.meiju.StatusEnum;
import com.zr.inquiry.meiju.TypeEnum;
import com.zr.inquiry.meiju.isTaxEnum;
import com.zr.inquiry.pojo.*;
import com.zr.inquiry.service.InquiryService;
import com.zr.lagalplant.pojo.LegalPerson;
import com.zr.util.AllRecords;
import com.zr.util.ResultVo;
import com.zr.util.ResultVoBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.*;

/**
 * Created by Hasee on 2019/10/9.
 */
@Service
public class InquriyServiceImpl implements InquiryService {

    @Autowired
    private InquiryMapper inquiryMapper;


    @Override
    public ResultVo add(@Valid InquiryAddVo inquiryAddVo) {
        //验证
        ResultVo resultVo=yanzheng(inquiryAddVo);
        if(!resultVo.getSuccess()){
            return resultVo;
        }
        //验证成功
         InquiryTotal inquiryTotal=new InquiryTotal();
        //给创建时间，人物
        //将左边的赋值给右边
        BeanUtils.copyProperties(inquiryAddVo,inquiryTotal);
        Date date=new Date();
        inquiryTotal.setCreatorName("小创");
        inquiryTotal.setCreateTime(date);
        inquiryTotal.setUpdateName("小创");
        inquiryTotal.setUpdateTime(date);
        //给单号，赋值，随机单号
        inquiryTotal.setRequestForQuotationId(UUID.randomUUID().toString());
        //入库
        //表头入库
        inquiryMapper.addTotal(inquiryTotal);
        //备件供应表入库
        //获取表头id，给备件供应表每个备件外键赋值
        List<Inquirysparedetail> inquirysparedetailList=inquiryAddVo.getInquirysparedetailList();
        for (Inquirysparedetail inquirysparedetail:inquirysparedetailList) {
            inquirysparedetail.setId(inquiryTotal.getId());
        }
        inquiryMapper.addBeiMingxi(inquirysparedetailList);
        //供应商供应表入库
        List<Inquirysupplierdetail> inquirysupplierdetailList=inquiryAddVo.getInquirysupplierdetailList();
        for (Inquirysupplierdetail inquirysupplierdetail:inquirysupplierdetailList) {
            inquirysupplierdetail.setId(inquiryTotal.getId());
        }
        inquiryMapper.addGongMingxi(inquirysupplierdetailList);

        return ResultVoBuilder.success();


    }

    @Override
    public ResultVo chabeijian(SparedetailClassVo sparedetailClassVo) {
        List<Inquirysparedetail> inquirysparedetailList=inquiryMapper.querySpare(sparedetailClassVo);
        return ResultVoBuilder.success(inquirysparedetailList);
    }

    @Override
    public ResultVo chagongying(SupplierClassVo supplierClassVo) {
        List<Inquirysupplierdetail> inquirysupplierdetailList=inquiryMapper.querySupplier(supplierClassVo);
        return  ResultVoBuilder.success(inquirysupplierdetailList);

    }

    @Override
    public ResultVo update(@Valid InquiryUpdateVo inquiryUpdateVo) {
        //验证
        InquiryAddVo inquiryAddVo=new InquiryAddVo();
        BeanUtils.copyProperties(inquiryUpdateVo,inquiryAddVo);
        ResultVo resultVo=yanzheng(inquiryAddVo);
        if(!resultVo.getSuccess()){
            return resultVo;
        }
        //验证成功
        InquiryTotal inquiryTotal=new InquiryTotal();
        //给创建时间，人物
        //将左边的赋值给右边
        BeanUtils.copyProperties(inquiryAddVo,inquiryTotal);
        Date date=new Date();
        inquiryTotal.setUpdateName("小创");
        inquiryTotal.setUpdateTime(date);
        //入库
        //表头入库
        inquiryMapper.updateTotal(inquiryTotal);
        //备件供应表入库
        //获取表头id，给备件供应表每个备件外键赋值
        List<Inquirysparedetail> inquirysparedetailList=inquiryUpdateVo.getInquirysparedetailList();
        for (Inquirysparedetail inquirysparedetail:inquirysparedetailList) {
            inquirysparedetail.setId(inquiryTotal.getId());
        }
        inquiryMapper.deleteBeiMingxi(inquiryTotal.getId());
        inquiryMapper.addBeiMingxi(inquirysparedetailList);
        //供应商供应表入库
        List<Inquirysupplierdetail> inquirysupplierdetailList=inquiryUpdateVo.getInquirysupplierdetailList();
        for (Inquirysupplierdetail inquirysupplierdetail:inquirysupplierdetailList) {
            inquirysupplierdetail.setId(inquiryTotal.getId());
        }
        inquiryMapper.deleteGongMingxi(inquiryTotal.getId());
        inquiryMapper.addGongMingxi(inquirysupplierdetailList);

        return ResultVoBuilder.success();
    }

    @Override
    public ResultVo queryPage(InquirySelectVo inquirySelectVo) {

        //查询数据
        List<InquiryTotal> inquiryTotalList = inquiryMapper.queryData(inquirySelectVo);
        //进行状态的格式转换
        for (InquiryTotal i : inquiryTotalList) {
            i.setInquiryStatusName(StatusEnum.getName(i.getInquiryStatus()));
        }
            //查询数量
            int count = inquiryMapper.queryCount(inquirySelectVo);
            AllRecords allRecords = new AllRecords();
            //将查到的数据存储到allRecords
            allRecords.setDataList(inquiryTotalList);
            //设置分页参数
            allRecords.setPageIndex(inquirySelectVo.getPageIndex());
            allRecords.setPageSize(inquirySelectVo.getPageSize());
            allRecords.setTotalNumber(count);
            allRecords.resetTotalPage(count);
            return ResultVoBuilder.success(allRecords);


        }

    @Override
    public ResultVo queryById(Integer id) {
        //头表回显
        InquiryTotal inquiryTotal = inquiryMapper.queryById(id);
        //将一些数据格式转换
        inquiryTotal.setCurrencyCodeName(CurrencyCodeEnum.getName(inquiryTotal.getCurrencyCode()));
        inquiryTotal.setInquiryStatusName(isTaxEnum.getName(inquiryTotal.getIsTax()));
        inquiryTotal.setTypeName(TypeEnum.getName(inquiryTotal.getType()));
        //备件明细表回显
        List<Inquirysparedetail> inquirysparedetailList = inquiryMapper.querySparePartByTotalId(id);
        //供应商明细表回显
        List<Inquirysupplierdetail> inquirysupplierdetailList = inquiryMapper.querySupplierByTotalId(id);
        //将备件和供应商明细表一起会先到头表中，之后返回头表即可回显所有内容
        inquiryTotal.setInquirysparedetailList(inquirysparedetailList);
        inquiryTotal.setInquirysupplierdetailList(inquirysupplierdetailList);
        return ResultVoBuilder.success(inquiryTotal);
    }

    private ResultVo yanzheng(@Valid InquiryAddVo inquiryAddVo) {
          /* 1.验证法人是否存在
        2.验证询价类型是否存在
        3.验证币别是否存在
        4.验证是否含税是否存在
        5.验证开始日期是否在结束日期之前*/

          ResultVo resultVo=yanzhengTotal(inquiryAddVo);
          if(!resultVo.getSuccess()){
              return  resultVo;
          }
          /*  6.验证备件是否存在，并且提示那个备件已经存在了
        7.验证备件是否重复，并且提示那个备件重复了
        8.验证备件是否存在可以生产它的供应商，并且提示用户哪个供应商可以生产它
       */

        ResultVo resultVo1=yanzhengbeijian(inquiryAddVo);
        if(!resultVo.getSuccess()){
            return  resultVo;
        }

         /*10.验证供应商是否存在，并且提示那个供应商已经存在了
        11.验证供应商是否重复，并且提示那个供应商重复了
        12.验证供应商是否存在可以生产的备件，并且提示用户哪个备件它可以生产
       */
         ResultVo resultVo2=yanzhenggongying(inquiryAddVo);
        if(!resultVo.getSuccess()){
            return  resultVo;
        }
        return ResultVoBuilder.success();
    }

    private ResultVo yanzhenggongying(@Valid InquiryAddVo inquiryAddVo) {
       /*10.验证供应商是否存在，并且提示那个供应商已经存在了
        11.验证供应商是否重复，并且提示那个供应商重复了
        12.验证供应商是否存在可以生产的备件，并且提示用户哪个备件它可以生产
       */
       //验证重复，包含
        // 获取供应商集合
       List<Inquirysupplierdetail> inquirysupplierdetailList=inquiryAddVo.getInquirysupplierdetailList();
       //新建遍历结果集合
        List<String> supplierList=new ArrayList<>();
       //将供应添加表里的code遍历到supplier
        for (Inquirysupplierdetail inquirysupplierdetail:inquirysupplierdetailList) {
            supplierList.add(inquirysupplierdetail.getVendorCode());
        }
        //新建重复集合
        List<String> chongfuList=new ArrayList<>();
        //新建遍历不重复的集合
        List<String> NoList=new ArrayList<>();
        //将supplier循环遍历
        for(String code:supplierList){
          //先给不存在任何的一个空Nolis赋code,如果赋值过程中有存在的存到cunzaiList里面
            if(NoList.contains(code)){
                chongfuList.add(code);
            }else{
                NoList.add(code);
            }
        }
       if(!chongfuList.isEmpty()){
            return ResultVoBuilder.error("500","重复的供应商为"+chongfuList);
       }

//        验证供应商是否存在，并且提示那个供应商重复了
        //将供应商的每个code遍历出来去查找供应商
        //查出来的供应商数量如果和所给的供应数量不一样就不存在，重复与否不影响大小
        List<String> code=inquiryMapper.querySupplierCodeList(supplierList);
       if(code.size()<supplierList.size()){
           supplierList.removeAll(code);  //将查到的全部减去，剩下没查到的
           return ResultVoBuilder.error("500","不存在的供应商为"+supplierList);
       }

//        12.验证供应商是否存在可以生产的备件，并且提示用户它可以生产哪个备件
        //获取备件供应表的所有备件
        List<Inquirysparedetail> inquirysparedetailList=inquiryAddVo.getInquirysparedetailList();
       //将这些备件循环遍历
        List<String> BeiList=new ArrayList<>();
        for (Inquirysparedetail inquirysparedetail:inquirysparedetailList) {
            //将每个备件编码添加到BeiList
            BeiList.add(inquirysparedetail.getSpareCoding());
        }
        //根据供应商查出所对应的备件
        //供应商集合 supplierList
        List<SpareSupplier> spareSupplierList=inquiryMapper.querySpareSpareByCodeList(supplierList);
        //根据中间表找出的备件集合
        List<String> spareList = new ArrayList<>();
        for (SpareSupplier spareSupplier: spareSupplierList){
            if (code.equals(spareSupplier.getVendorCode())){
                spareList.add(spareSupplier.getSpareCoding()); //将备件信息添加进spareList
            }
        }
        //判断是否存在交集，如果存在交集为false 不存在为true
        if (Collections.disjoint(spareSupplierList,spareList)){
            return ResultVoBuilder.error("500","供应商编码："+code+"不存在合法的北极那，它的合法备件为："+spareList);
        }
        return ResultVoBuilder.success();

    }

    private ResultVo yanzhengbeijian(@Valid InquiryAddVo inquiryAddVo) {
         /*  6.验证备件是否存在，并且提示那个备件已经存在了
        7.验证备件是否重复，并且提示那个备件重复了
        8.验证备件是否存在可以生产它的供应商，并且提示用户哪个供应商可以生产它
        */
        //备件集合
        List<Inquirysparedetail> inquirysparedetailList = inquiryAddVo.getInquirysparedetailList();
        List<String> codeList = new ArrayList<>();
        List<String> repeatCodeList = new ArrayList<>();
        List<String> repeatNewCodeList = new ArrayList<>();
        for (Inquirysparedetail inquirysparedetail:inquirysparedetailList){
            codeList.add(inquirysparedetail.getSpareCoding());
        }
        for (String code :codeList){
            if (repeatCodeList.contains(code)){
                repeatNewCodeList.add(code);
            }else {
                repeatCodeList.add(code);
            }
        }
        if (!repeatNewCodeList.isEmpty()){
            return ResultVoBuilder.error("500","存在重复的备件编码，重复的备件编码为:"+repeatNewCodeList);
        }
        //验证是否存在
        List<String> codes = inquiryMapper.querySparePartByCodeList(codeList);
        if (codes.size()!=codeList.size()){
            codeList.removeAll(codes);
            return ResultVoBuilder.error("500","不存在的备件编码为："+codeList);
        }
        //验证备件是否存在合法的供应商
        List<Inquirysupplierdetail> inquirysupplierdetailList = inquiryAddVo.getInquirysupplierdetailList();
        List<String> supplierCodeList = new ArrayList<>();
        for (Inquirysupplierdetail inquirysupplierdetail:inquirysupplierdetailList){
            supplierCodeList.add(inquirysupplierdetail.getVendorCode());//将供应商code添加进supplierCodeList
        }
        //1.现根据备件集合查询出这些备件对应的供应商信息
        List<SpareSupplier> spareSupplierList = inquiryMapper.querySpareSupplierByCodeList(codeList);
        for  (String code :codeList){
            //根据中间表找出的供应商集合
            List<String> vendorList = new ArrayList<>();
            for (SpareSupplier spareSupplier: spareSupplierList){
                if (code.equals(spareSupplier.getSpareCoding())){
                    vendorList.add(spareSupplier.getVendorCode());
                }
            }
            //判断是否存在交集，如果存在交集为false 不存在为true
            if (Collections.disjoint(supplierCodeList,vendorList)){
                return ResultVoBuilder.error("500","备件编码："+code+"不存在合法的供应商，它的合法供应商为："+vendorList);
            }
        }



        return ResultVoBuilder.success();
    }

    private ResultVo yanzhengTotal(@Valid InquiryAddVo inquiryAddVo) {
        /* 1.验证法人是否存在
        2.验证询价类型是否存在
        3.验证币别是否存在
        4.验证是否含税是否存在
        5.验证开始日期是否在结束日期之前*/

        //法人
        LegalPerson legalPerson=inquiryMapper.queryLegalPerson(inquiryAddVo.getLegalPersonId());
        if(legalPerson==null){
            return  ResultVoBuilder.error("500","法人不存在");
        }
        //币别
        if(CurrencyCodeEnum.getName(inquiryAddVo.getCurrencyCode())==null){
            return ResultVoBuilder.error("500","币别 不存在");
        }
        if (TypeEnum.getName(inquiryAddVo.getType())==null){
            return ResultVoBuilder.error("500","询价类型不存在！");
        }
        if (isTaxEnum.getName(inquiryAddVo.getIsTax())==null){
            return ResultVoBuilder.error("500","询价类型不存在！");
        }
        if(inquiryAddVo.getStartTime().after(inquiryAddVo.getEndTime())){
            return ResultVoBuilder.error("500","开始日期应在结束日期之前");
        }

        return ResultVoBuilder.success();
    }


}
